<?php
require_once 'CMS/Util.php';
require_once 'CMS/Pagination.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';

/*
Người tạo	: Lại Ngọc Thái Châu - Team 0
Module 	: Comment Product
Chức năng	: Show + Post Comment
 */
 
class Comment_CommentController extends BaseUserController{
	private $pagination;
	public function init(){
		parent::init();

	}

	// ham xu ly viec kiem tra captcha va post comment
	public function postcommentAction(){
		$this->pagination = new Pagination();
		$dbUser = new Comment_Model_Users();
		$dbComment = new Comment_Model_Comment();
		$dateInt = new Util();
			
		if(isset($_GET['id']))
		{
			if(is_numeric($_GET["id"]))
			{
				$id = $_GET["id"];
				$countComment = $dbComment->countCommentbyID($id);
				if (!isset($_GET["page"]) || is_numeric($_GET["page"])!=true ||$_GET["page"]<0) 
					$page = 1;
	            else 
	            	$page = $_GET["page"];
	            	
	            $limit = 5;
	           
	            $dataComment = $dbComment->getCommentbyID($id, $page, $limit);
	    
				$i=0;
				if($dataComment!=null)
				{
					foreach($dataComment as $data)
					{
						$comment[$i]=array(
						"comment_id" =>$data['comment_id'],
						"title" =>$data['title'],
						"description" =>$data['description'],
						"created_date" =>$dateInt->makeDays($data['created_date']),
						"updated_date" =>$dateInt->makeDays($data['updated_date']),
						"user_id" =>$data['user_id'],
						"user_link" =>$data['user_link'],
						"user_image" =>$data['user_image'],
						"user_name" =>$data['user_name'],
						"yahoo" =>$data['yahoo'],
						"skype" =>$data['skype'],
						);
						$i++;
					}
						
				}
					
				if(isset($countComment) && $countComment >0) {
					
					echo "<h2>Danh sách trả lời <b class=\"count\">".$countComment."</b></h2>";
					
					if (isset($comment))
					{
						foreach($comment as $com)
						{
							echo "<div id=\"reply_".$com['comment_id']."\">";
							echo "<div class=\"user\">";
							echo "<div class=\"avatar_xx_small\"><a rel=\"nofollow\" target=\"_blank\" href=\"".$com['link_user']."\" iuse=\"828180\" class=\"tooltip_user\">";
							echo "<img width=\"50\" height=\"47\" src=\"".$com['user_image']."\" maxheight=\"50\" maxwidth=\"50\"></a></div>";
							echo "<div class=\"loginname\"><a rel=\"nofollow\" target=\"_blank\" href=\"".$com['link_user']."\" title=\"".$com['user_name']."\" class=\"text_link\">".$com['user_name']."</a></div>";
							echo "<div class=\"opt end\">";
							if(isset($com['yahoo'])) {
							echo"<a href=\"ymsgr:SendIM?".$com['yahoo']."\" title=\"".$com['yahoo']."\"><img src=\"http://opi.yahoo.com/online?u=".$com['yahoo']."&amp;t=5&amp;l=us\"></a>";
							 } 
							if(isset($com['skype'])) {
							echo"<a href=\"skype:".$com['skype']."?chat\" title=\"".$com['skype']."\"><img src=\"/images/comment/icon_skype.gif\"></a></div>";
							 }
							echo"</div><div class=\"data\"><div class=\"arrow\"></div><div class=\"description\"><div class=\"reply_description\">".($com['description'])."</div><br><div class=\"break_module_line\"></div></div>";
							
							echo"<div class=\"information\"><div class=\"magin_top\"><span>Ngày gửi:</span>".$com['created_date']."&nbsp;<span class=\"sep\">|</span>&nbsp;<span>Ngày Cập Nhật:</span>". $com['updated_date']."</div></div></div><div class=\"break_content_line\" ></div></div>";
						}
					}
				} 
				else 
				{
					echo "<div style=\"text-align: center;\"><span >Không có comment nào</span></div>";
				}
				
				// khoi tao lai phan trang
				$this->pagination->init("/commentproduct/postcomment?id=". $_GET["id"]  ."&page=", $countComment, $limit, $page);
				// show phan trang
				echo "<div style=\"text-align: center;\">".$this->pagination->renderJson()."</div>";
				   
				echo"<a onclick='showreply()' href=\"javascript:;\" class=\"reply_btn\"><span>Trả lời</span></a>";
				$temp="";
				$this->_helper->json(213);	
			}
		}
		else 
		{
			//Start------Lấy thông tin captcha từ session
			$session = new Zend_Session_Namespace('login.auth');
		
			$captchaID = $_REQUEST["code"];
						
			$txtCaptcha =$_REQUEST["security_code"];

			$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaID);
			         	
			$captchaIterator = $captchaSession->getIterator();
			         	
			$captchaWord = $captchaIterator['word'];  	
			if($captchaWord==null && $session->current_code!=null )
				$captchaWord = $session->current_code;
			//End--------Lấy thông tin captcha từ session
			
			//Start------Kiểm tra captcha nhập từ form			
			if(strtolower($txtCaptcha) != strtolower($captchaWord)){
							
				$flag = false;
							
				$session->current_code = $captchaWord;
							
				$data =-1;
							
				$this->_helper->json($data);
						
			}		
			else 		
			{
							
				$temp = array(
							
					"thing_id"=>$_REQUEST["thing_id"],
							
					"user_id"=>$session->id_user,
							
					"title"=>"",
							
					"description"=>$_REQUEST["content"],
							
					"created_date"=>time(),
							
					"updated_date"=>time()
						
				);
						
				//print_r($temp);
				$data = $dbComment->insertComment($temp);
						
					
				$this->_helper->json($data);
						
			}
			//End--------Kiểm tra captcha nhập từ form
		}
	}	
	
	//delete Comment
	public function deleteAction()
	{
		//$response="Del comment id ".$_GET["id"];
		//$this->_helper->json($response);
		$path = APPLICATION_PATH."/widgets/comment/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy_content($xmlconfig->name);
		if($hasmodify!="true")
		{
			$data=-1;
			$this->_helper->json($data);	
			return;
		}
		if(isset($_GET["id"]))
		{
			$dbComment = new Comment_Model_Comment();
			$dbComment->deleteCommentByID($_GET["id"]);
		}
		
	}
	
}
?>